const http = require('http')
const url = require('url')

const server = http.createServer((request, response) => {
  console.log('获取请求方法', request.method)

  console.log('获取http协议版本', request.httpVersion)

  console.log('获取请求路径', request.url)

  console.log('获取请求头', request.headers)

  let body = ''
  request.on('data', chunk => {
    body += chunk
  })
  request.on('end', () => {
    console.log('获取请求体', body)
  })

  console.log('url路径', url.parse(request.url).pathname)
  console.log('url查询字符串', url.parse(request.url, true).query)

  const urlObj = new URL(request.url, 'http:localhost:9000')
  console.log('url路径（新）', urlObj.pathname)
  console.log('url查询字符串（新）', urlObj.searchParams.get('name'))

  response.setHeader('Content-Type', 'text/html;charset=utf-8')
  response.end('你好~')
})

server.listen(9000, () => {
  console.log('服务已启动，9000端口监听中。。。')
})